<?php

namespace app\manage\controller;

use app\common\controller\Manage;
use app\common\model\LiquorOrder as liquorOrderModel;

class LiquorOrder extends Manage
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        return $this->fetch();
    }
    
    public function api()
    {
        $page = $this->request->get('page', 1);
        $limit = $this->request->get('limit', 15);
        $condition = [];
        if($this->request->get('order_bn')) {
            $condition[] = ['order_bn', '=', $this->request->get('order_bn')];
        }
        if($this->request->get('status')) {
            $condition[] = ['status', '=', $this->request->get('status')];
        }
        $liquorOrderModel = new liquorOrderModel();
        $liquor_order_list = $liquorOrderModel::with(['liquor', 'user'])->where($condition)->limit(($page-1)*$limit, $limit)->select();
        $liquor_order_count = $liquorOrderModel->where($condition)->count();
        return [
            'code' => 0,
            'count' => $liquor_order_count,
            'data' => $liquor_order_list
        ];
    }
    
    public function issue() 
    {
        $liquorOrderModel = new liquorOrderModel();
        $liquorOrderModel->where('id', $this->request->post('id'))->setField('status', 1);
        return [
            'status'    => true,
            'msg'       => '出单成功'
        ];
    }
    
    public function cancel()
    {
        $liquorOrderModel = new liquorOrderModel();
        return $liquorOrderModel->cancel($this->request->post('id'));
    }
    
    public function getLiquorOrder() 
    {
        $liquorOrderModel = new liquorOrderModel();
        $data = $liquorOrderModel::with(['user', 'liquor'])->where('id', $this->request->post('id'))->find();
        $data['ctime2'] = date('Y-m-d H:i:s', $data['ctime']);
        return [
            'status'    =>  true,
            'data'      =>  $data
        ];
    }
}
